<!--
 * @Descripttion: 支付配置
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-11-19 18:38:59
-->
<template>
  <div class="lb-malls-payment-setting">
    <top-nav></top-nav>
    <div class="page-main">
      <el-tabs type="border-card">
        <el-tab-pane label="支付设置">
          <el-form
            @submit.native.prevent
            :model="setForm"
            :rules="setFormRules"
            ref="setForm"
            label-width="140px"
          >
            <el-form-item label="商户号" prop="mch_id">
              <el-input
                v-model="setForm.mch_id"
                placeholder="请输入商户号"
              ></el-input>
            </el-form-item>
            <el-form-item label="支付秘钥" prop="pay_key">
              <el-input
                v-model="setForm.pay_key"
                placeholder="请输入支付秘钥"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <lb-button type="primary" @click="submitFormInfo('setForm')">{{
                $t('action.submit')
              }}</lb-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="上传文件">
          <el-form
            @submit.native.prevent
            :model="setForm"
            :rules="setFormRules"
            ref="uploadForm"
            label-width="140px"
          >
            <el-form-item label="cert_path" prop="cert_path">
              <div class="upload-file-warp">
                <div class="upload-file-warp-content">
                  {{ setForm.cert_path | handleFileName }}
                </div>
                <lb-button
                  class="lb-cover-wrap"
                  size="mini"
                  type="primary"
                  @click="handleBtnUplaod('cert_path')"
                  >{{ $t('action.upload') }}</lb-button
                >
              </div>
            </el-form-item>
            <el-form-item label="key_path" prop="key_path">
              <div class="upload-file-warp">
                <div class="upload-file-warp-content">
                  {{ setForm.key_path | handleFileName }}
                </div>
                <lb-button
                  size="mini"
                  type="primary"
                  @click="handleBtnUplaod('key_path')"
                  >{{ $t('action.upload') }}</lb-button
                >
              </div>
            </el-form-item>
            <el-form-item>
              <lb-button type="primary" @click="submitFormInfo('uploadForm')">{{
                $t('action.submit')
              }}</lb-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
    <input
      type="file"
      accept="text"
      style="display: none"
      @change="uploadFiles"
      ref="inputFile"
    />
  </div>
</template>

<script>
export default {
  data () {
    let checkPayKey = (rule, value, callback) => {
      let reg = /^[0-9a-zA-Z]*$/g
      if (!reg.test(value)) {
        callback(new Error('请输入字母数字类型'))
      } else {
        callback()
      }
    }
    return {
      fileName: '',
      setForm: {
        mch_id: '',
        pay_key: '',
        cert_path: '',
        key_path: ''
      },
      setFormRules: {
        mch_id: {
          required: true,
          type: 'string',
          message: '请输入商户号',
          trigger: 'blur'
        },
        pay_key: [
          {
            required: true,
            type: 'string',
            message: '请输入支付秘钥',
            trigger: 'blur'
          },
          { validator: checkPayKey, trigger: 'blur' }
        ],
        cert_path: {
          required: true,
          type: 'string',
          message: '请上传证书',
          trigger: 'change'
        },
        key_path: {
          required: true,
          type: 'string',
          message: '请上传证书',
          trigger: 'change'
        }
      }
    }
  },
  created () {
    this.getFormInfo()
  },
  methods: {
    handleBtnUplaod (name) {
      this.fileName = name
      this.$refs['inputFile'].click()
    },
    uploadFiles (e) {
      let file = e.target.files[0]
      let { name } = file
      if (name.indexOf('pem') < 0) {
        this.$message.error('请上传pem格式的文件')
      } else {
        let formData = new FormData()
        formData.append('file[]', file)
        formData.append('type', 'cert')
        this.$api.uploadFiles(formData).then(res => {
          if (res.code === 200) {
            this.$message.success(this.$t('tips.successUpload'))
            this.setForm[this.fileName] = res.data[0].attachment
          }
        })
      }
    },
    getFormInfo () {
      this.$api.paymentSetting().then(res => {
        if (res.code === 200) {
          for (let key in this.setForm) {
            this.setForm[key] = res.data[key]
          }
        }
      })
    },
    submitFormInfo (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          let subForm = JSON.parse(JSON.stringify(this.setForm))
          this.$api.paymentSetting(subForm).then(res => {
            if (res.code === 200) {
              this.$message.success(this.$t('tips.successSub'))
            }
          })
        }
      })
    },
    handleFileUrl (url) {
      if (url) {
        return url.substring(url.lastIndexOf('cert') + 1)
      } else {
        return ''
      }
    }
  },
  watch: {
    'subForm.cert_path' (newValue, oldValue) {
      if (newValue && !oldValue) {
        this.$refs['setForm'].clearValidate('cert_path')
      }
    },
    'subForm.key_path' (newValue, oldValue) {
      if (newValue && !oldValue) {
        this.$refs['setForm'].clearValidate('key_path')
      }
    }
  },
  filters: {
    handleFileName (url) {
      if (url) {
        return url.substring(url.lastIndexOf('/') + 1)
      } else {
        return '请上传文件'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-malls-payment-setting {
  width: 100%;
  .page-main {
    .el-input {
      width: 300px;
    }
    .upload-file-warp {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    .upload-file-warp-content {
      width: 85%;
      display: inline-block;
    }
  }
}
</style>
